module.exports = function rikoView (model, template, engine) {

  rikoView.engine = engine || require('virtual-dom');

  var mainloop = require("main-loop")(model(), render, rikoView.engine);
  var _update = mainloop.update;
  mainloop.update = function () {
    try {
      _update.apply(mainloop, arguments);
    } catch (e) {
      console.error(e, "when updating view with", arguments)
    }
  }

  model(function (state) { mainloop.update(state) })
  return mainloop;

  function render (state) {
    try {
      return template(state)
    } catch (e) {
      console.error(e, "when rendering template", template, "with state", state);
      return rikoView.engine.h(".error",
        [ rikoView.engine.h("h1", "template error")
        , rikoView.engine.h("pre", e.message)
        , rikoView.engine.h("pre", e.stack) ])
    }
  }
}
